package com.datastructure.practice;

import java.util.Arrays;
import java.util.Map;

public class Salvation1 {

    public static void main(String[] args) {
        System.out.println(new Solution().convert("PAYPALISHIRING",3));
    }
}

class Solution {
    public String convert(String s, int numRows){
        if(numRows==1){
            return s;
        }
        StringBuilder sb = new StringBuilder();
        int sum = s.length();
        int numOfBox = 2 * numRows + numRows-2;
        int column = (sum/(numOfBox-numRows)+1)*(numRows-1);
        int time = 0;
        int multiply = 0;
        char [][] data = new char[numRows][column];
        for(int i = 0; i<column; i++){
            multiply=i/(numRows-1)+1;
            for(int j = 0; j<numRows; j++){
                if((i%(numRows-1)!=0)&&(j+i)!=((numRows-1)*multiply)){
                    continue;
                }
                if(time<sum) {
                    data[j][i] = s.charAt(time);
                }
                time++;
            }
        }
        for(int i = 0; i<numRows; i++){
            for(int j = 0; j<column; j++){
                if(data[i][j]!=' '){
                    sb.append(data[i][j]);
                }
            }
        }
       return sb.toString().replace("\u0000","");
    }
}
